package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.jiyi.adapter.CommonAdapter;
import com.jiyi.adapter.ViewHolder;
import com.jiyi.bean.datainput.new_input.DataInputStreetBean;
import com.jiyi.bean.datainput.new_input.DataInputZoneBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/4/25.
 */

public class StreetSearchActivity extends ActivityBase {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.ivHint)
    ImageView ivHint;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    @BindView(R.id.spStreet)
    Spinner spStreet;
    @BindView(R.id.spZone)
    Spinner spZone;

    List<DataInputStreetBean> streetDataList = new ArrayList<DataInputStreetBean>();
    List<DataInputZoneBean> zoneDataList = new ArrayList<DataInputZoneBean>();

    CommonAdapter<DataInputStreetBean> stateFirstAdapter;
    CommonAdapter<DataInputZoneBean> stateSecondAdapter;

    String street,zone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_street_search);
        ButterKnife.bind(this);
        init();
    }

    void init()
    {
        setTop();
        setAdapter();
        getFirst();
    }

    private void setTop() {
        tvTitle.setText("街道选择");
        tvBack.setText("");
        tvSearch.setVisibility(View.GONE);
    }

    private void setAdapter() {
        resetFirst();
        stateFirstAdapter = new CommonAdapter<DataInputStreetBean>(this, streetDataList, R.layout.item_spinner) {

            @Override
            public void convert(ViewHolder holder, DataInputStreetBean t) {
                holder.setText(R.id.state_name, t.getName());
            }
        };
        spStreet.setAdapter(stateFirstAdapter);
        spStreet.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                resetSecond();
                //做个判断，如果上级菜单传给我们的是空id，即上级未选择选项，则我们不去获取下级的菜单
                if (streetDataList.size() >0 && !streetDataList.get(position).getName().equals("请选择")) {
                    getSecond(streetDataList.get(position).getName());
                    street = streetDataList.get(position).getName();
                }
                else
                {
                    stateSecondAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        resetSecond();
        stateSecondAdapter = new CommonAdapter<DataInputZoneBean>(this, zoneDataList, R.layout.item_spinner) {

            @Override
            public void convert(ViewHolder holder, DataInputZoneBean t) {
                holder.setText(R.id.state_name, t.getName());
            }
        };
        spZone.setAdapter(stateSecondAdapter);
        spZone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //做个判断，如果上级菜单传给我们的是空id，即上级未选择选项，则我们不去获取下级的菜单
                if (zoneDataList.size() >0 && !zoneDataList.get(position).getName().equals("请选择")) {
                    zone = zoneDataList.get(position).getName();
                    Intent intent = new Intent(StreetSearchActivity.this, SMDataInputListActivity.class);
                    intent.putExtra("street",street);
                    intent.putExtra("zone",zone);
                    startActivity(intent);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void resetFirst()
    {
        street = "";
        streetDataList.clear();
        DataInputStreetBean head = new DataInputStreetBean();
        head.setName("请选择");
        streetDataList.add(head);
    }

    private void resetSecond()
    {
        zone = "";
        zoneDataList.clear();
        DataInputZoneBean head = new DataInputZoneBean();
        head.setName("请选择");
        zoneDataList.add(head);
        spZone.setSelection(0);
    }

    /**
     * @author: HYM
     * @Title: getFirst
     * @Description: TODO(得到一级设备的列表)    void
     * @date: 2017-5-11
     */
    private void getFirst()
    {
        HttpMethods.getInstance().getDataInputStreet(UserInfoUtil.getCurrentUserAccessToken()).subscribe(
                new SampleProgressObserver<List<DataInputStreetBean>>(this) {
                    @Override
                    public void onNext(List<DataInputStreetBean> dataInputStreetBeans) {
                        if (dataInputStreetBeans.size() > 0) {
                            streetDataList.addAll(dataInputStreetBeans);
                            stateFirstAdapter.notifyDataSetChanged();
                        }
                    }
                }
        );
    }

    private void getSecond(String streetName)
    {
        HttpMethods.getInstance().getDataInputZone(UserInfoUtil.getCurrentUserAccessToken(),streetName).subscribe(
                new SampleProgressObserver<List<DataInputZoneBean>>(this) {
                    @Override
                    public void onNext(List<DataInputZoneBean> dataInputZoneBean) {
                        if (dataInputZoneBean.size() > 0) {
                            zoneDataList.addAll(dataInputZoneBean);
                            stateSecondAdapter.notifyDataSetChanged();
                        }
                    }
                }
        );
    }

    @OnClick({R.id.tvBack, R.id.tvSearch})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;

            default:
                break;
        }
    }
}
